package com.tql.view.config;
import com.tql.view.pojo.User;
import com.tql.view.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;

public class UserRealm extends AuthorizingRealm {
    @Autowired
    UserService service;
    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
       /* SimpleAuthorizationInfo info=new SimpleAuthorizationInfo();
        //获取当前用户
        Subject subject= SecurityUtils.getSubject();
        User user= (User) subject.getPrincipal();//拿到user对象
       *//* //授予权限
        info.addStringPermission(user.getPerms());*//*
        info.addRole(user.getRole());*/
        return null;
       /* SimpleAuthorizationInfo info=new SimpleAuthorizationInfo();
        info.addStringPermission("user:add");*//*
        *//*return info;*/
        /*return null;*/
    }
    //认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        UsernamePasswordToken token= (UsernamePasswordToken) authenticationToken;
       User user=service.getUserByAccount(token.getUsername());
       if(user==null){
           return null;
       }
       Subject subject=SecurityUtils.getSubject();
       Session session=subject.getSession();
       session.setAttribute("user",user);
        //封装密码确认
        return new SimpleAuthenticationInfo(user,user.getPassword(),"");
    }
}
